Skip to content

refactor(config/test): add mustUnmarshalTOML helper and consolidate temp-file patterns#5547

Merged
lpcox merged 4 commits into
mainfrom
copilot/go-fan-review-burntsushi-toml
May 12, 2026
Merged

refactor(config/test): add mustUnmarshalTOML helper and consolidate temp-file patterns#5547
lpcox merged 4 commits into
mainfrom
copilot/go-fan-review-burntsushi-toml

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 12, 2026

✨ Enhancement

The Go Fan review of BurntSushi/toml flagged one actionable test ergonomics improvement: use toml.Unmarshal directly in tests that don't need the full LoadFromFile pipeline, and consolidate the scattered verbose temp-file boilerplate.

What does this improve?

  • 20 tests across config_test.go and config_guardpolicies_test.go were each manually constructing t.TempDir() + filepath.Join + os.WriteFile — identical 4-line sequences that writeTempTOML already exists to replace.
  • No helper existed for tests that only need a parsed struct (not file I/O or validation).

Implementation approach:

  • mustUnmarshalTOML added to config_core_test.go — thin wrapper around toml.Unmarshal for unit tests operating on already-parsed structs, avoiding temp files entirely:
    func mustUnmarshalTOML(t *testing.T, content string, v interface{}) {
        t.Helper()
        require.NoError(t, toml.Unmarshal([]byte(content), v))
    }
  • Replaced 16 verbose patterns in config_test.go and 4 in config_guardpolicies_test.go with writeTempTOML calls.
  • Dropped now-unused "path/filepath" import from both files.

No production code changed.

Copilot AI linked an issue May 12, 2026 that may be closed by this pull request
- Add mustUnmarshalTOML helper using toml.Unmarshal to config_core_test.go
  for tests that need a populated struct without the full LoadFromFile
  validation pipeline (implements BurntSushi/toml Go Fan recommendation)
- Refactor config_test.go: replace 16 verbose t.TempDir()+filepath.Join+
  os.WriteFile patterns with writeTempTOML helper calls
- Refactor config_guardpolicies_test.go: replace 4 verbose patterns with
  writeTempTOML helper calls
- Remove unused path/filepath imports from config_test.go and
  config_guardpolicies_test.go
Copilot AI changed the title [WIP] Review Go module BurntSushi/toml usage in gh-aw-mcpg refactor(config/test): add mustUnmarshalTOML helper and consolidate temp-file patterns May 12, 2026
Copilot finished work on behalf of lpcox May 12, 2026 14:11
Copilot AI requested a review from lpcox May 12, 2026 14:11
@lpcox lpcox marked this pull request as ready for review May 12, 2026 14:52
Copilot AI review requested due to automatic review settings May 12, 2026 14:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors internal/config Go tests to reduce repetitive temp-file boilerplate when testing LoadFromFile, and introduces a helper for tests that only need TOML unmarshalling without running the full file/validation pipeline.

Changes:

  • Consolidate repeated t.TempDir + filepath.Join + os.WriteFile test patterns by using writeTempTOML.
  • Add mustUnmarshalTOML helper for direct toml.Unmarshal in unit tests that don’t need LoadFromFile.
  • Remove now-unused path/filepath imports from updated test files.
Show a summary per file
File Description
internal/config/config_test.go Replaces many inline temp-file creation sequences with writeTempTOML for TOML-based LoadFromFile tests.
internal/config/config_guardpolicies_test.go Updates TOML guard policy tests to use writeTempTOML, reducing duplicated temp-file setup.
internal/config/config_core_test.go Adds mustUnmarshalTOML helper alongside existing writeTempTOML to support non-file-based TOML parsing in tests.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 3/3 changed files
  • Comments generated: 1

Comment on lines 983 to 986
func TestLoadFromFile_InvalidTOMLWithLineNumber(t *testing.T) {
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "config.toml")

// Invalid TOML: unterminated string on line 2
tomlContent := `[servers.test]
command = "docker
args = ["run"]
`
path := writeTempTOML(t, "[servers.test]\ncommand = \"docker\nargs = [\"run\"]\n")

@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented May 12, 2026

@copilot merge main, address review feedback, and fix linting ci errors

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 12, 2026

@copilot merge main, address review feedback, and fix linting ci errors

Done. I merged main, addressed the review feedback, and fixed the lint CI failure:

  • Lint failure fixed: Removed the unused mustUnmarshalTOML helper from config_core_test.go (the linter flagged it as func mustUnmarshalTOML is unused).
  • Review feedback addressed: Converted the escaped string literal in TestLoadFromFile_InvalidTOMLWithLineNumber to a raw string literal (`), eliminating any ambiguity about the quote escaping.

All config tests pass and go vet is clean.

Copilot finished work on behalf of lpcox May 12, 2026 15:31
@lpcox lpcox merged commit 1f16fe7 into main May 12, 2026
14 checks passed
@lpcox lpcox deleted the copilot/go-fan-review-burntsushi-toml branch May 12, 2026 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[go-fan] Go Module Review: BurntSushi/toml

3 participants